package com.koi.system.controller.post;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.koi.common.anno.KoiLog;
import com.koi.common.anno.KoiPostDataScope;
import com.koi.common.enums.log.OperType;
import com.koi.common.ex.koi.YUtil;
import com.koi.common.satoken.KoiLoginUtil;
import com.koi.model.bo.element.SelectLongBo;
import com.koi.model.vo.system.SysPostVo;
import com.koi.pojo.system.SysPost;
import com.koi.system.service.post.SysPostService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ･ᴗ･
 * @description [岗位信息表-控制层]
 */
@Validated
@RequestMapping("/koi")
@RequiredArgsConstructor
@RestController
public class SysPostController {

    private final SysPostService sysPostService;

    /**
     * @description [分页查询]
     * @author ･ᴗ･
     */
    @KoiPostDataScope(postAlias = "t1")
    @SaCheckPermission("system:post:list")
    @GetMapping("/sysPost/listPage")
    public IPage<SysPost> listPage(SysPostVo vo) {
        return sysPostService.listPage(vo);
    }

    /**
     * @description [查询所有]
     * @author ･ᴗ･
     */
    @GetMapping("/sysPost/list")
    public List<SysPost> list() {
        return sysPostService.list();
    }

    /**
     * @description [查询一个]
     * @author ･ᴗ･
     */
    @GetMapping("/sysPost/getById/{id}")
    public SysPost getById(@PathVariable Long id) {
        return sysPostService.getById(id);
    }

    /**
     * @description [新增]
     * @author ･ᴗ･
     */
    @KoiLog(title = "新增岗位", operType = OperType.ADD)
    @SaCheckPermission("system:post:add")
    @PostMapping("/sysPost/add")
    public void add(@RequestBody SysPost sysPost) {
        if (StringUtils.isNotBlank(KoiLoginUtil.getUserName())) {
            sysPost.setCreateBy(KoiLoginUtil.getUserName());
        }
        YUtil.isTrue(!sysPostService.save(sysPost), "新增失败，请稍后重试");
    }

    /**
     * @description [修改]
     * @author ･ᴗ･
     */
    @KoiLog(title = "修改岗位", operType = OperType.UPDATE)
    @SaCheckPermission("system:post:update")
    @PostMapping("/sysPost/update")
    public void update(@RequestBody SysPost sysPost) {
        if (StringUtils.isNotBlank(KoiLoginUtil.getUserName())) {
            sysPost.setUpdateBy(KoiLoginUtil.getUserName());
        }
        YUtil.isTrue(!sysPostService.updateById(sysPost), "修改失败，请稍后重试");
    }

    /**
     * @description [删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "删除岗位", operType = OperType.DELETE)
    @SaCheckPermission("system:post:delete")
    @PostMapping("/sysPost/deleteById/{id}")
    public void delete(@PathVariable Long id) {
        YUtil.isTrue(!sysPostService.removeById(id), "删除失败，请稍后重试");
    }

    /**
     * @description [批量删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "批量删除岗位", operType = OperType.DELETE)
    @SaCheckPermission("system:post:delete")
    @PostMapping("/sysPost/batchDelete")
    public void batchDelete(@NotNull(message = "请选择需要删除的数据") @RequestBody List<Long> ids) {
        YUtil.isTrue(!sysPostService.removeByIds(ids), "删除失败，请稍后重试");
    }

    /**
     * @description [修改状态]
     * @author ･ᴗ･
     */
    @PostMapping("/sysPost/updateStatus/{postId}/{postStatus}")
    public void updateStatus(@PathVariable("postId") Long postId, @PathVariable("postStatus") String postStatus) {
        UpdateWrapper<SysPost> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("post_status", postStatus).eq("post_id", postId);
        YUtil.isTrue(!sysPostService.update(updateWrapper), "修改失败，请稍后重试");
    }

    /**
     * @description [查询所有正常岗位-穿梭框]
     * @author ･ᴗ･
     */
    @SaCheckPermission("system:user:post")
    @GetMapping("/sysPost/listNormalPost/{userId}")
    public Map<String, Object> listNormalPost(@PathVariable("userId") Long userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("data1", sysPostService.listLeftPost());
        map.put("data2", sysPostService.listRightPost(userId));
        return map;
    }

    /**
     * @description [根据当前用户ID分配岗位-穿梭框]
     * @author ･ᴗ･
     */
    @KoiLog(title = "分配岗位", operType = OperType.AUTH)
    @SaCheckPermission("system:user:post")
    @GetMapping("/sysPost/assignUserPost/{userId}/{postIds}")
    public void assignUserPost(@PathVariable("userId") Long userId, @PathVariable("postIds") List<Long> postIds) {
        sysPostService.assignUserPost(userId, postIds);
    }

    /**
     * @description [获取岗位下拉框]
     * @author ･ᴗ･
     */
    @GetMapping("/sysPost/listPostElSelect")
    public List<SelectLongBo> listPostElSelect() {
        return sysPostService.listPostElSelect();
    }

}
